3. Format Tag

fmt 태그를 사용하기 위해서 페이지 상단에 다음과 같이 선언되어야 된다.
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>

다국어 문서를 처리할 때 유용하고, 날짜와 숫자 형식을 다루는 fmt 태그는 다음과 같은 종류가 있다.

기능태그prefix
Locale 설정setLocale, requestEncodingfmt
메시지 처리bundle, message(param), setBundlefmt
숫자 날짜 형식formatNumber, formatDate, parseDate, parseNumber, setTimeZone, timeZonefmt

3.1 <fmt:setLocale/>

사용자 세션에 대한 로케일 설정


<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<pre>
default locale : <%= response.getLocale() %>
set locale : ko <fmt:setLocale value="ko" />
now: <%= response.getLocale() %>
set locale : ja <fmt:setLocale value="ja" />
now: <%= response.getLocale() %>
set locale : en <fmt:setLocale value="en" />
now: <%= response.getLocale() %>
</pre>

3.2 <fmt:requestEncoding/>

request.setCharacterEncoding() 역할


<%@ page contentType="text/html;charset=euc-kr" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<fmt:requestEncoding value="euc-kr"/>
파라메터:<c:out value="${param.id}"/>
<form method="post">
    <input type="text" name="id">
    <input type="submit">
</form>

3.3 <fmt:bundle/>

properties 확장자를 사용하는 자원 파일을 읽어오는 역할

<fmt:bundle basename="basename"
[WEBSTUDY:prefix="prefix"]>
body content
</fmt:bundle>

3.4 <fmt:message/>

번들 태그에서 정한 값들을 가져옴.

<fmt:message key="messageKey"
[WEBSTUDY:bundle="resourceBundle"]
[WEBSTUDY:var="varName"]
[scope="{page\
WEBSTUDY:request|session|application}"]/>

TestBundle.properties


greeting=Hello.
admin=kenu

TestBundle_ko.properties


greeting=안녕하세요.
admin=허광남


<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<fmt:setLocale value="ko"/>
<fmt:bundle var="testBundle" basename="com.itexpert.chap9.msg.TestBundle">
 <fmt:message>greeting</fmt:message><br>
 <fmt:message bundle="${testBundle}" key="admin"/><br>
</fmt:bundle>

3.5 <fmt:setBundle/>

페이지 전체에서 사용할 수 있는 번들을 지정.
var 속성에서 정한 변수를 이후에 나오는 <fmt:message/> 태그에서 basename 속성에 변수명으로 대체 가능.

3.6 <fmt:formatNumber/>

특정 로케일 방식이나, 통화, 퍼센트를 포함하는 숫자 데이타를 디스플레이.
정수와 소수점을 구분하는데 마침표나 콤마를 사용할 것인지 설정.

<fmt:formatNumber value="numericValue"
[type="{number\
WEBSTUDY:currency|percent}"]
[WEBSTUDY:pattern="customPattern"]
[WEBSTUDY:currencyCode="currencyCode"]
[WEBSTUDY:currencySymbol="currencySymbol"]
[groupingUsed="{true\
WEBSTUDY:false}"]
[WEBSTUDY:maxIntegerDigits="maxIntegerDigits"]
[WEBSTUDY:minIntegerDigits="minIntegerDigits"]
[WEBSTUDY:maxFractionDigits="maxFractionDigits"]
[WEBSTUDY:minFractionDigits="minFractionDigits"]
[WEBSTUDY:var="varName"]
[scope="{page\
WEBSTUDY:request|session|application}"]/>
속성동적값Type설명
valuetrueString 또는Number형식화될 수치
typetrueString숫자, 통화, 퍼센트 중 어느 것으로 표시할 지 지정 {number|currency|percent}
patterntrueString사용자가 지정한 형식 패턴.
currencyCodetrueStringISO 4217 통화 코드. 통화 형식일 때만 적용\(type="currency")
currencySymboltrueString통화 기호. 통화 형식일 때만 적용\(type="currency")
groupingUsedtrueboolean형식 출력에 그룹 분리기호를 포함할지 여부
maxIntegerDigitstrueint형식 출력에서 integer 최대 자리수
minIntegerDigitstrueint형식 출력에서 integer 최소 자리수
maxFractionDigitstrueint형식 출력에서 소수점 이하 최대 자리수.
minFractionDigitstrueint형식 출력에서 소수점 이하 최소 자리수.
varfalseString형식 출력 결과 문자열을 담는 scope에 해당하는 변수명
scopefalseStringvar 의 scope

3.7 <fmt:parseNumber/>

value 애트리부트나 바디 콘텐트를 통해 제공된 숫자 데이타를 파싱.

<fmt:parseNumber value="numericValue"
[type="{number\
WEBSTUDY:currency|percent}"]
[WEBSTUDY:pattern="customPattern"]
[WEBSTUDY:parseLocale="parseLocale"]
[integerOnly="{true\
WEBSTUDY:false}"]
[WEBSTUDY:var="varName"]
[scope="{page\
WEBSTUDY:request|session|application}"]/>
속성동적값Type설명
valuetrueString 또는Number파싱할 수치
typetrueString숫자, 통화, 퍼센트 중 어느 것으로 표시할 지 지정 {number|currency|percent}
patterntrueString사용자가 지정한 형식 패턴.
parseLocaletrueString 또는java.util.Locale파싱 작업의 기본 형식 패턴(숫자, 통화, 퍼센트 각각)을 제공하는 Locale
integerOnlytrueboolean주어진 값에서 integer 부분만 파싱할지 여부를 지정
varfalseString파싱 결과 (java.lang.Number 타입)를 담는 scope에 해당하는 변수명
scopefalseStringvar 의 scope

3.8 <fmt:formatDate/>

날짜와 시간을 포맷 및 디스플레이 하는데 사용

<fmt:formatDate value="date"
[type="{time\
WEBSTUDY:date|both}"]
[dateStyle="{default\
WEBSTUDY:short|medium|long|full}"]
[timeStyle="{default\
WEBSTUDY:short|medium|long|full}"]
[WEBSTUDY:pattern="customPattern"]
[WEBSTUDY:timeZone="timeZone"]
[WEBSTUDY:var="varName"]
[scope="{page\
WEBSTUDY:request|session|application}"]/>
속성동적값Type설명
valuetruejava.util.Date형식화될 Date 와 time
typetrueString형식화할 데이터가 시간, 날짜, 모두 인지 셋 중 하나를 지정한다.
dateStyletrueString미리 정의된 날짜 형식. Java.text.DateFormat 클래스에 정의된 문법을 따른다. type="date", type="body", type속성이 생략된 경우 사용.
timeStyletrueString미리 정의된 날짜 형식. Java.text.DateFormat 클래스에 정의된 문법을 따른다. type="time", type="body" 의 경우 사용.
patterntrueString사용자 지정 형식 스타일
timeZonetrueString 또는java.util.TimeZone형식화 시간에 나타날 타임존
varfalseString형식 출력 결과 문자열을 담는 scope에 해당하는 변수명
scopefalseStringvar 의 scope

3.9 <fmt:parseDate/>

날짜와 시간 값을 파싱하는데 사용

<fmt:parseDate value="dateString"
[type="{time\
WEBSTUDY:date|both}"]
[dateStyle="{default\
WEBSTUDY:short|medium|long|full}"]
[timeStyle="{default\
WEBSTUDY:short|medium|long|full}"]
[WEBSTUDY:pattern="customPattern"]
[WEBSTUDY:timeZone="timeZone"]
[WEBSTUDY:parseLocale="parseLocale"]
[WEBSTUDY:var="varName"]
[scope="{page\
WEBSTUDY:request|session|application}"]/>
속성동적값Type설명
valuetruejava.util.Date파싱할 Date 와 time
typetrueString파싱할 데이터가 시간, 날짜, 모두 인지 셋 중 하나를 지정한다.
dateStyletrueString미리 정의된 날짜 형식. Java.text.DateFormat 클래스에 정의된 문법을 따른다. type="date", type="body", type속성이 생략된 경우 사용.
timeStyletrueString미리 정의된 날짜 형식. Java.text.DateFormat 클래스에 정의된 문법을 따른다. type="time", type="body" 의 경우 사용.
patterntrueString사용자 지정 형식 스타일
timeZonetrueString 또는java.util.TimeZone형식화 시간에 나타날 타임존
parseLocaletrueString 또는java.util.Locale파싱하는 동안 적용될 미리 정의된 형식 스타일의 Locale
varfalseString파싱 결과(java.util.Date)를 담는 scope에 해당하는 변수명
scopefalseStringvar 의 scope

<c:set var="koDateString">06. 4. 1 오후 7:03</c:set>
<fmt:parseDate value="${koDateString}" parseLocale="ko_KR" 
    type="both" dateStyle="short" timeStyle="short"
    var="koDate"/>
<c:set var="usDateString">4/1/06 7:03 PM</c:set>
<fmt:parseDate value="${usDateString}" parseLocale="en_US" 
    type="both" dateStyle="short" timeStyle="short"
    var="usDate"/>
<c:set var="gbDateString">4/1/06 19:03</c:set>
<fmt:parseDate value="${gbDateString}" parseLocale="en_GB" 
    type="both" dateStyle="short" timeStyle="short"
    var="gbDate"/>

<table border="1">
<tr>
    <th>Locale</th>
    <th>input</th>
    <th>output</th>
</tr>
<tr>
    <td>Korea Korean(ko_KR)</td>
    <td><c:out value="${koDateString}"/></td>
    <td><c:out value="${koDate}"/></td>
</tr>
<tr>
    <td>U.S. English(en_US)</td>
    <td><c:out value="${usDateString}"/></td>
    <td><c:out value="${usDate}"/></td>
</tr>
<tr>
    <td>British English(en_GB)</td>
    <td><c:out value="${gbDateString}"/></td>
    <td><c:out value="${gbDate}"/></td>
</tr>

3.10 <fmt:setTimeZone/>, <fmt:timeZone/>

기본 시간대 값을 설정.

문서에 대하여